#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../../lib'
require 'rubygems'
require 'drb/drb'
require 'rbconfig'
require 'spec'

module Spec
  module Runner
    class RailsSpecServer
      def run(args, stderr, stdout)
        ::Dispatcher.reset_application!
        ::Dependencies.mechanism = :load
        require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
    
        $context_runner = OptionParser.create_context_runner(args, false, stderr, stdout)
        args.each do |file_or_dir|
          if File.directory?(file_or_dir)
            Dir["#{file_or_dir}/**/*.rb"].each do |file| 
              load file
            end
          else
            load file_or_dir
          end
        end
        $context_runner.run(false) unless args.empty?
      end
    end
  end
end
puts "Loading Rails environment"

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'dispatcher'

def restart_test_server
  puts "restarting"
  config       = ::Config::CONFIG
  ruby         = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
  command_line = [ruby, $0, ARGV].flatten.join(' ')
  exec(command_line)
end  

trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
puts "Ready"
DRb.start_service("druby://localhost:8989", Spec::Runner::RailsSpecServer.new)
DRb.thread.join